#include "widget.h"
#include "ui_widget.h"

void widget::setName(QString na)
{
    name=na;
}

void widget::init_sock()
{
    ts=new QTcpSocket(this);
    connect(ts,&QTcpSocket::readyRead,this,&widget::receivemes);
    ts->connectToHost(address,8081);
    int pos=address.lastIndexOf(".");
    int num=(address.sliced(pos+1)).toInt()+1;
    address=address.sliced(0,pos+1)+QString::number(num);
}

void widget::receivemes()   //come/mes/leave name: mes  //信息格式定义
{
    QString res=ts->readAll();
    int pos=res.indexOf(" ",0);
    QString type=res.sliced(0,pos);
    if(type=="come")
    {
        deal_come(res.sliced(pos+1));
    }
    else if(type=="mes")
    {
        deal_mes(res.sliced(pos+1));
    }
    else if(type=="leave")
    {
        deal_leave(res.sliced(pos+1));
    }
    else if(type=="people")
    {
        deal_people(res.sliced(pos+1));
    }
}

QString widget::gaintime()
{
    QTextStream out(stdout);
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString time=currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    return time;
}

void widget::sendmes(QString type,QString str)  //type表示信息的类型
{
    //发送信息的格式:  类型 名字: mes
    str=type+" "+name+":"+str;
    //发送信息到服务器上
    ts->write(str.toUtf8());
    //在plianedit上也显示发送内容
    // ui->plainTextEdit->appendPlainText(str_display.toUtf8());
}

void widget::deal_come(QString str)
{

    QString time=gaintime();
    int pos=str.indexOf(":",0);
    QString new_name=str.sliced(1,pos-1);
    QString mes=time+" "+new_name+":"+"进入到了房间";
    if(new_name!=name)
    ui->plainTextEdit->appendPlainText(mes.toUtf8());
    //在listWidget上显示进入的用户
    bool empty=ui->listWidget->findItems(new_name,Qt::MatchExactly).isEmpty();
    if(empty)
    {
        ui->listWidget->insertItem(0,new_name);
    }
}

void widget::deal_leave(QString str)
{
    QString time=gaintime();
    int pos=str.indexOf(":",0);
    QString new_name=str.sliced(0,pos);
    QString mes=time+" "+str;
    ui->plainTextEdit->appendPlainText(mes.toUtf8());
    //在listWidget上删除进入的用户
    deletePeopleFromlistWidget(new_name);
}

void widget::deal_mes(QString str)
{
    QString time=gaintime();
    str=time+" "+str;
    ui->plainTextEdit->appendPlainText(str.toUtf8());
}

void widget::deal_people(QString people)
{
    int left=0;
    for(int i=0;i<people.size();i++)
    {
        if(people[i]==' ')
        {
            QString str=people.sliced(left,i-left);
            bool empty=ui->listWidget->findItems( str,Qt::MatchExactly).isEmpty();
            if(empty)
            {
                 ui->listWidget->addItem(str);
            }
            left=i+1;
        }
    }
}

void widget::deletePeopleFromlistWidget(QString str)    //通过名字删除某行
{
    int count=ui->listWidget->count();
    int pos=0;
    if(count>0)
    {
        while(pos<count)
        {
            QListWidgetItem* item=ui->listWidget->item(pos);
            if(item->text()==str)
            {

                ui->listWidget->takeItem(pos);
                break;
            }
            pos++;
        }

    }
}



widget::widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::widget)
{
    ui->setupUi(this);
    init_sock();
    sendmes("come ",name+": 进入房间了");
}

widget::widget(QString na)
    : QWidget(nullptr)
    , ui(new Ui::widget)
    ,name(na)
{
    ui->setupUi(this);
    ui->textEdit->setFocus();
    init_sock();
    ui->label->setText(name);
    sendmes("come ","进入房间了");
}


widget::~widget()
{
    delete ui;
}

void widget::on_pushButton_send_clicked()
{
    QString str=ui->textEdit->toPlainText();
    sendmes("mes",str);
    ui->textEdit->clear();
}


void widget::on_pushButton_clicked()
{
    //发送用户退出的信息
     sendmes("leave","退出了房间");
    //发送用户退出的信号给menu
    emit this->closeWidget();
    // 关闭套接字
    ts->disconnectFromHost();
    ts->deleteLater();
    // 关闭界面
    this->close();
}

